<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_frdownload.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        //$this->setState('limit', $limit);
        $this->setState('limit', 5);
        $this->setState('limitstart', $limitstart);
    }
function getAll($list = null){
        
       $qr = "SELECT distinct CADM_CODE,CADM_NAME
                      FROM cadm  inner join dowi on DOWI_CADM_CODE=CADM_CODE 
                                left join kc2vr_users on dowi.DOWI_USEM_CODE=kc2vr_users.id                              
              ";
                    
        $where = array();
        if(($list['DOWI_NAME']) != null ){
        $where[] = "DOWI_NAME LIKE '%$list[DOWI_NAME]%'";
        }
        if(($list['USER']) != null ){
        $where[] = "username LIKE '%$list[USER]%'";
        }
        //search
        if(($list['CADM_CODE']) !=0 ){
                   $where[] = "CADM_CODE = $list[CADM_CODE]";  
        }

       if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }
       // get total number record
       $this->_total = $this->getTotal($where);
         
       $qr .= $where;
 
       
       // order by
       $qr .= " ORDER BY CADM_CODE DESC"; 
       // pagination
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
          
        //load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       //load data info
       if($result !=null)
       {
        foreach($result as $row)
            {
                $a= $row->CADM_CODE;
               // $qr = "SELECT DOWI_NAME,DOWI_DESCRIPTION,username FROM  DOWI LEFT JOIN kc2vr_users ON kc2vr_users.id=DOWI.DOWI_USEM_CODE";
                $qr = "SELECT DOWI_CODE,DOWI_NAME,DOWI_DESCRIPTION,DOWI_PATH,DOWI_DATE,username 
                                FROM  dowi left join kc2vr_users on dowi.DOWI_USEM_CODE=kc2vr_users.id ";
                $where = array();
                  //search
                $where[]="DOWI_CADM_CODE=$a";               
                    if(($list['DOWI_NAME']) != null ){
                    $where[] = "DOWI_NAME LIKE '%$list[DOWI_NAME]%'";
                    }
                    if(($list['USER']) != null ){
                    $where[] = "username LIKE '%$list[USER]%'";
                    }
                   if($where){
                        $where = ' WHERE '.implode(' AND ', $where);
                    }else{
                        $where = null;
                    }
                   $qr .= $where;
                   
                   // order by
                   $qr .= " ORDER BY DOWI_CODE DESC"; 
                  
                   $this->_db->setQuery($qr);
                   $data = $this->_db->loadObjectList();
                   $row->data = $data;
                 
            }
            }
        
       return $result;
	}
    // get total
    function getTotal($where = null){
        // Load the content if it doesn't already exist
        if ($this->_total === null) {
            
            $query = "SELECT count(distinct CADM_CODE)
                             FROM cadm  inner join dowi on DOWI_CADM_CODE=CADM_CODE 
                                        left join kc2vr_users on dowi.DOWI_USEM_CODE=kc2vr_users.id  
                                        $where";
            //echo $query;
//            return;
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    } 
    
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    /**
     * get dowi path from dowi_code
    **/
    function getDowiPath($cid){
        $qr = "SELECT DOWI_PATH
                        FROM dowi
                        WHERE DOWI_CODE = '$cid'";
       
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       return $result->DOWI_PATH;
    }  
 
 
 /**
     * check  dowi path from dowi_code
    **/
    function checkDowiPath($name){
        $qr = "SELECT DOWI_PATH
                        FROM dowi
                        WHERE DOWI_NAME = '$name'";
       
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();  
       return $result;
    } 
    
    
    //load all CADM
    function getAllCadm(){

	   $qr = "SELECT * FROM cadm";
       // order by
       $qr .= " ORDER BY CADM_CODE DESC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	} 
  
  
}
?>
